﻿Imports System.IO
Public Class frmDSGB
    Dim _tk As New TimKiem
    Dim _vd As New dsVong
    Dim _td As New dsTranDau
    Dim _clb As New dsCLB
    Dim _gb As New dsGhiBan
    Private _matranGlobal As Integer
    Dim path As String = Directory.GetCurrentDirectory

    Dim ctx As New QLDBDataContext
    Sub New(ByVal MaTranGlobal As Integer)
        InitializeComponent()
        _matranGlobal = MaTranGlobal
    End Sub
    Private Sub btnDong_Click(sender As Object, e As EventArgs) Handles btnDong.Click
        Close()
    End Sub

    Sub LayTran(ByVal MV As Integer)
        cboCapDau.DataSource = _td.LayCapDau(MV)
        cboCapDau.DisplayMember = "CapDau"
        cboCapDau.ValueMember = "MaTran"
    End Sub

    Sub LayDoi(ByVal MaTran As Integer)
        cboDoi.DataSource = _clb.LayDoi(MaTran)
        cboDoi.DisplayMember = "TenCLB"
        cboDoi.ValueMember = "MaCLB"

    End Sub

    Private Sub LayVong()
        cboVongDau.DataSource = _vd.LayDSVong()
        cboVongDau.DisplayMember = 0
        cboVongDau.ValueMember = "Vong"
    End Sub

    Private Sub frmDSGB_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Cursor = Cursors.AppStarting
        LayVong()
        If dgvDSGB.DataSource Is Nothing Then
            CaiDatGridView()
        End If
        If _tk.LayDSGhiBan(_matranGlobal).Count = 0 Then
            MessageBox.Show("Không Có Bàn Thắng Nào Được Cập Nhật Cho Trận Đấ Bạn Mới Chọn ")
            dgvDSGB.DataSource = _gb.LayDanhSachGB()

        Else
            dgvDSGB.DataSource = _tk.LayDSGhiBan(_matranGlobal)
        End If
        Me.Cursor = Cursors.Default
    End Sub

    Sub CaiDatGridView()
        With dgvDSGB
            .ColumnCount = 4
            .Columns(0).DataPropertyName = "Phut"

            .Columns(1).DataPropertyName = "TenCT"

            .Columns(2).DataPropertyName = "TenCLB"

            .Columns(3).DataPropertyName = "MaTran"
        End With
    End Sub

    Private Sub cboVongDau_SelectedValueChanged(sender As Object, e As EventArgs) Handles cboVongDau.SelectedValueChanged
        Try
            LayTran(cboVongDau.SelectedValue)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub cboCapDau_SelectedValueChanged(sender As Object, e As EventArgs) Handles cboCapDau.SelectedValueChanged
        Try
            LayDoi(cboCapDau.SelectedValue)
        Catch ex As Exception

        End Try
    End Sub

    Private Function LayDSCauThu(ByVal MaDoi As Integer) As IList
        Dim v = From q In ctx.CAUTHUs Where q.MaCLB = MaDoi
                Order By q.MaCT
                Select New With {.HoTen = q.HoTen, .MaCT = q.MaCT}
        Return v.ToList()
    End Function

    Private Sub LayCT(ByVal MD As Integer)
        cboCauThu.DataSource = LayDSCauThu(MD)
        cboCauThu.DisplayMember = "HoTen"
        cboCauThu.ValueMember = "MaCT"
    End Sub

    Private Sub cboDoi_SelectedValueChanged(sender As Object, e As EventArgs) Handles cboDoi.SelectedValueChanged
        Try
            LayCT(cboDoi.SelectedValue)
        Catch ex As Exception

        End Try
    End Sub

    Sub them()
        Dim GhiBan As New DANHSACHGHIBAN
        Dim GB As New DANHSACHGHIBAN
        Dim TG As Integer
        TG = Integer.Parse(txtPhut.Text.ToString())
        Dim MT As Integer = cboCapDau.SelectedValue
        GB = ctx.DANHSACHGHIBANs.SingleOrDefault(Function(p) p.ThoiDiem = TG And p.MaTran = MT)
        If Not GB Is Nothing Then
            MessageBox.Show("Dữ Liệu Bạn Nhập Đã Tồn Tại, Vui Lòng Kiểm Tra Lại")
            Return
        End If
        GhiBan.MaCLB = cboDoi.SelectedValue
        GhiBan.MaTran = cboCapDau.SelectedValue
        GhiBan.MaCT = cboCauThu.SelectedValue
        GhiBan.ThoiDiem = Integer.Parse(txtPhut.Text.ToString())
        ctx.DANHSACHGHIBANs.InsertOnSubmit(GhiBan)
        ctx.SubmitChanges()
        dgvDSGB.DataSource = _gb.LayDanhSachGB()
        ReLoad()

    End Sub

    Private Sub btnThem_Click(sender As Object, e As EventArgs) Handles btnThem.Click
        If txtPhut.Text = "" Or IsNumeric(txtPhut.Text) = False Or Integer.Parse(txtPhut.Text.ToString()) > 120 Then
            MessageBox.Show("Kiểm Tra Lại Thời Gian Ghi Bàn")
            txtPhut.Focus()
            Return
        Else
            them()
            MessageBox.Show("Thêm Thành Công")
        End If
    End Sub

    Private Sub SelectCel()
        Dim DSGB As New dsGhiBan
        DSGB = dgvDSGB.CurrentRow.DataBoundItem()
        lbGio.Text = DSGB.Phut
        lbMT.Text = DSGB.MaTran
        txtPhut.Text = DSGB.Phut
        cboCapDau.SelectedValue = DSGB.MaTran

    End Sub

    Private Sub dgvDSGB_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvDSGB.CellContentClick
        SelectCel()
    End Sub

    Sub ReLoad()
        lbGio.Text = String.Empty
        lbMT.Text = String.Empty
        txtPhut.Text = String.Empty
        txtPhut.Focus()

    End Sub

    Sub Xoa()
        Dim Toup As New DANHSACHGHIBAN
        Dim mt As Integer
        mt = Integer.Parse(lbMT.Text.ToString())
        Dim tg As Integer
        tg = Integer.Parse(lbGio.Text.ToString())
        Toup = ctx.DANHSACHGHIBANs.SingleOrDefault(Function(p) p.MaTran = mt And p.ThoiDiem = tg)
        ctx.DANHSACHGHIBANs.DeleteOnSubmit(Toup)
        ctx.SubmitChanges()
        dgvDSGB.DataSource = _gb.LayDanhSachGB()
    End Sub
    Sub Sua()
        Xoa()
        them()

    End Sub

    Private Sub btnChinhSua_Click(sender As Object, e As EventArgs) Handles btnChinhSua.Click
        If lbMT.Text = "" Then
            MessageBox.Show("Cần Chọn Một Dòng Trước Khi Sửa")
            Return
        Else
            Sua()
            MessageBox.Show("Cập Nhật Thành Công")
        End If
    End Sub

    Private Sub dgvDSGB_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvDSGB.CellClick
        SelectCel()
    End Sub

    Private Sub dgvDSGB_CellContentDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvDSGB.CellContentDoubleClick
        SelectCel()
    End Sub

    Private Sub dgvDSGB_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvDSGB.CellDoubleClick
        SelectCel()
    End Sub

    Private Sub btnXoa_Click(sender As Object, e As EventArgs) Handles btnXoa.Click
        If lbMT.Text = "" Then
            MessageBox.Show("Cần Chọn Một Dòng Trước Khi Xóa")
            Return
        Else
            Xoa()
            ReLoad()
            MessageBox.Show("Xóa Thành Công")
        End If
    End Sub

    Private Sub likGiupDo_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles likGiupDo.LinkClicked
        hlDSDB.HelpNamespace = path + "\hlp\index.html#DoiBong"
        Help.ShowHelp(lb, hlDSDB.HelpNamespace.ToString())
    End Sub
End Class